package homework.Homework09;

public class Test {
    public static void main(String[] args) {
        Person[] person = new Person[4];
        person[0] = new Student("jack", '男', 12, "001");
        person[1] = new Student("marry", '女', 14, "002");
        person[2] = new Teacher("tom", '男', 54, 10);
        person[3] = new Teacher("milan", '女', 42, 20);

        Test test = new Test();
        test.bubbleSort(person);

        //输出排序后的数组
        for(int i = 0; i < person.length; i++) {
            System.out.println(person[i]);
        }

        //遍历数组，调用hi方法
        for (int i = 0; i < person.length; i++) {
            test.hi(person[i]);
        }
    }

    //完成年龄从高到低排序
    public void bubbleSort(Person[] persons) {
        Person tmp = null;
        for(int i = 0; i < persons.length - 1; i++) {
            for(int j = 0; j < persons.length - 1 - i; j++) {
                if(persons[j].getAge() < persons[j + 1].getAge()) {
                    tmp = persons[j];
                    persons[j] = persons[j + 1];
                    persons [j + 1] = tmp;
                }
            }
        }
    }

    public void hi(Person p) {
        if(p instanceof Student) {
            ((Student)p).study();
        } else if(p instanceof Teacher) {
            ((Teacher)p).teach();
        }
    }
}
